package com.abccat.myapplication;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import DataStruct.ArrayList;
import DataStruct.Asserts;
import DataStruct.ListNode;
import DataStruct.SingleLinkedList;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        testSingleLinkedList();




        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
    }

    // 判断链表是否有环.
    private boolean hasCycle(ListNode head) {
        if (head == null || head.next == null) return false;
        ListNode slow = head;
        ListNode fast = head.next;
        while (slow != fast) {
            if (fast == null || fast.next == null) return false;
            slow = slow.next;
            fast = fast.next.next;
        }
        return true;
    }

    // LeetCode 的ListNode进行反转.
    private ListNode reversalListNode(ListNode head) {
//        if (head == null || head.next == null) {
//            Log.d("reversalListNode", "ListNode为单节点或为null,无需反转!");
//            return head;
//        }
        ListNode newHeade = null;
        if (head != null) {
            ListNode tmp = head.next;
            head.next = newHeade;
            newHeade = head;
            head = tmp;
        }
        return newHeade;
    }

    // 反转链表
    private void reversalList() {
        SingleLinkedList linkedList = new SingleLinkedList<>();
        linkedList.add(11);
        linkedList.add(12);
        linkedList.add(13);
        linkedList.add(14);
        Log.d("SingleLinkedList", linkedList.toString());
        Log.d("SingleLinkedList", linkedList.printReverList());
    }

    private void testSingleLinkedList() {
        SingleLinkedList linkedList = new SingleLinkedList<>();
        linkedList.add(11);
        linkedList.add(12);
        linkedList.add(13);
        linkedList.add(14);
        linkedList.set(1, 21);
        linkedList.add(0, "qq");
        linkedList.remove(3);
        Log.d("SingleLinkedList", linkedList.toString());

        for (int i = 0; i < linkedList.size(); i++) {
            Log.d("SingleLinkedList", "" + linkedList.get(i));
            Log.d("SingleLinkedList", "" + linkedList.indexOf(linkedList.get(i)));
        }

        linkedList.remove(3);
        linkedList.remove(2);


//        linkedList.clear();

        Log.d("SingleLinkedList", linkedList.toString());
    }

    private void testArrayList() {
        ArrayList list = new ArrayList();

        Log.d("ArrayList", list.isEmpty()? "空数组" : "非空数组");

        for (int i = 0; i < 17; i++) {
            list.add(i);
        }

        Log.d("ArrayList", "数组长度:" + list.length() + ", 数组元素个数:" + list.size());

        Log.d("ArrayList", list.isEmpty()? "空数组" : "非空数组");

        Log.d("ArrayList",list.toString());
        list.remove(2);
        Log.d("ArrayList",list.toString());
        list.remove(5);
        Log.d("ArrayList",list.toString());
        list.remove(7);
        Log.d("ArrayList",  "" + list.size());

//        for (int i = 0; i < 10; i++) {
//            list.remove(0);
//        }
//        Log.d("ArrayList", "数组长度:" + list.length() + ", 数组元素个数:" + list.size());
//
//        list.add(2, "qq");
//        list.add(list.size(), "cc");
//        Log.d("ArrayList",list.toString());
//
//        list.remove(0);
//        Log.d("ArrayList",list.toString());
//
        list.clear();
        Log.d("ArrayList", "数组长度:" + list.length() + ", 数组元素个数:" + list.size());
        Asserts.test(10 == list.length());
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
